/**
 * Created by zengy on 2017/9/4.
 */

angular.module('mainApp.wecodeCount',['ui.router'])
    .config(function ($stateProvider) {
        $stateProvider
            .state({
                name:'wecodeCount/wecodeList',
                url:'/wecodeCount/wecodeList',
                templateUrl:'views/wecodeCount/wecode_list.html?v='+new Date().getMinutes(),
                controller:'wecodeCountCtrl'
            })
    })
    .controller('wecodeCountCtrl',function ($scope) {
        $scope.$emit('arg',13);
        var date=new Date();
        $scope.codeCountModel={
            "endTime":date.getFullYear() + "-" + (date.getMonth()+1) + "-" + date.getDate(),
            "startTime":date.getFullYear() + "-" + (date.getMonth()) + "-" + date.getDate(), // 开始时间（yyyy-mm-dd HH:mm:ss）
            "channelName":"", // 渠道名称
            "goodsId":"", // 服务包id(传goodsId的值)
            "pageNo":1 ,// 页数（从1开始）
            "pageSize":"10", // 每页大小
        };
        $('.date-picker').datepicker({
            autoclose: true,
            changeMonth: true,
            language : 'zh-CN',
            format : 'yyyy-mm-dd',
            showOptions: {direction: 'up'}
        });
        //获取服务包信息
        new HttpRequest().queryGoodsList(function (res) {
            if(res.code == '000'){
                $scope.goodsList=res.data;
                $scope.$apply();
            }
        });
        $scope.pagination = function (currentPage, pageCount) {
            $('.page-function #pagination').remove();
            $('.page-function').html('<ul id="pagination" class="pagination"></ul>');
            $('.pagination').twbsPagination({
                totalPages: pageCount,
                startPage: currentPage,
                visiblePages: 5,
                first: "首页",
                prev: "上一页",
                next: "下一页",
                last: "尾页",
                href: 'javascript:;',
                //hideOnlyOnePage : true,
                onPageClick: function (event, page) {
                    $scope.getCodeList(page);
                }
            });
        };
        $scope.getCodeList=function (page) {
            if(page){
                $scope.codeCountModel.pageNo=page;
            };
            if(!$scope.codeCountModel.startTime && $scope.codeCountModel.endTime){
                layer.msg('时间筛选不不符合规则！');
                return false;
            }
            if(parseInt(new Date($scope.codeCountModel.startTime).getTime()/1000) > parseInt(new Date($scope.codeCountModel.endTime).getTime()/1000)){
                layer.msg('开始时间不能大于结束时间！');
                return false;
            }
            new HttpRequest().conductQrcode($scope.codeCountModel,function (res) {
                if(res.code =='000'){
                    $scope.codeList=res.data.list;
                    $scope.$apply();
                    if($scope.codeList.length>0){
                        $scope.pagination($scope.codeCountModel.pageNo, res.data.pageCount)
                    }
                }else{
                    layer.msg(res.msg);
                }
            })
        }
        $scope.getCodeList($scope.codeCountModel.pageNo);
        $scope.exportExcel=function () {
            delete $scope.codeCountModel.pageNo;
            delete $scope.codeCountModel.pageSize;
            var arg="";
            for(var attr in $scope.codeCountModel){
                if($scope.codeCountModel[attr]){
                    arg+=(attr+'='+$scope.codeCountModel[attr]+'&');
                }
            }
            arg=arg+'token='+localStorage.token+'&userId='+localStorage.userId;
            location.href=baseUrl+'/channelManager/expertConductQrcode?'+arg;

        }
    })
